package Model;

import org.codehaus.jackson.annotate.JsonIgnore;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by zl206 on 2016/11/6.
 */
public class snake  {
    private String id;
    private ConcurrentLinkedQueue<point> points=new  ConcurrentLinkedQueue<>();
    private String color=Tool.color();
    private int img=new Random().nextInt(7);
    private double angle=0;
    private point head;
    private double headSize;
    private final int style=new Random().nextInt(4);
    @JsonIgnore
    private int score=0;
    @JsonIgnore
    private AtomicInteger loseTime=new AtomicInteger(0);
    @JsonIgnore
    private AtomicInteger loseSpeed=new AtomicInteger(0);
    @JsonIgnore
    private bg bg;

    public snake() {
    }

    public snake(bg bg,String id) {
        this.id=id;
        this.bg=bg;
        reset();
    }



    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public ConcurrentLinkedQueue<point> getPoints() {
        return points;
    }

    public void setPoints(ConcurrentLinkedQueue<point> points) {
        this.points = points;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }

    public double getAngle() {
        return angle;
    }

    public void setAngle(double angle) {
        this.angle = angle;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public double getHeadSize() {
        return headSize;
    }

    public void setHeadSize(double headSize) {
        this.headSize = headSize;
    }

    public int getLoseTime() {
        return loseTime.get();
    }

    public void setLoseTime(int loseTime) {
        this.loseTime.set(loseTime);
    }

    public void lostLoseTime(int i){
        this.loseTime.addAndGet(-i);
    }


    public int getLoseSpeed() {
        return loseSpeed.get();
    }

    public void setLoseSpeed(int loseSpeed) {
        this.loseSpeed.set(loseSpeed);
    }

    public void lostLoseSpeed(int i){
        this.loseSpeed.addAndGet(-i);
    }

    public int getStyle() {
        return style;
    }


    public point getHead() {
        return head;
    }




    public void setHead(point head) {
        this.head = head;
        this.points.add(head);
        setHeadSize();
    }





    public void addScore(int score){
        this.score +=score;
    }

    public boolean dropScore(){
        if (this.score>=this.headSize){
            this.score-=headSize;
            return true;
        }
        return false;
    }

    private void setHeadSize(){
        this.headSize=10+this.points.size()/50;
    }

    public void dropLength(){
        this.points.poll();
        setHeadSize();
    }
    public boolean isOut(int w,int h){
        if (this.head.getX()-this.headSize<0||
                this.head.getY()-this.headSize<0||
                this.head.getX()+this.headSize>w||
                this.head.getY()+this.headSize>h){
            return true;
        }
        return false;
    }

    public void reset(){
        this.points=new ConcurrentLinkedQueue<>();
        this.head=new point(bg.getWidth(),bg.getHeight());
        this.points.add(this.head);
        for (int i=1;i<30;++i){
            this.head=Tool.forward(head,3,new Random().nextDouble()*Math.PI);
            this.points.add(this.head);
        }
        this.headSize = 10+this.points.size()/50;
        this.score=0;
        this.loseSpeed.set(0);
        this.loseTime.set(0);
    }
}
